package com.ruoyi.cms.controller;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.cms.domain.CmsSiteBanner;
import com.ruoyi.cms.domain.CmsSiteInfo;
import com.ruoyi.cms.domain.CmsSiteModule;
import com.ruoyi.cms.domain.CmsSiteVisitorMessage;
import com.ruoyi.cms.service.ICmsSiteBannerService;
import com.ruoyi.cms.service.ICmsSiteInfoService;
import com.ruoyi.cms.service.ICmsSiteModuleService;
import com.ruoyi.cms.service.ICmsSiteVisitorMessageService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;


@Controller
public class CmsWebController extends BaseController {
    @Autowired
    private ICmsSiteInfoService cmsSiteInfoService;
    @Autowired
    private ICmsSiteBannerService cmsSiteBannerService;
    @Autowired
    private ICmsSiteModuleService cmsSiteModuleService;
    @Autowired
    private ICmsSiteVisitorMessageService cmsSiteVisitorMessageService;


    private CmsSiteInfo handPubData(Model model) {
        CmsSiteInfo info = cmsSiteInfoService.selectUseCmsSiteInfo();
        CmsSiteBanner cmsSiteBanner = new CmsSiteBanner();
        cmsSiteBanner.setModuleId((long)1);
        List<CmsSiteBanner> head = cmsSiteBannerService.selectCmsSiteBannerList(cmsSiteBanner);//获取头部信息
        cmsSiteBanner.setModuleId((long)2);
        List<CmsSiteBanner> footer = cmsSiteBannerService.selectCmsSiteBannerList(cmsSiteBanner);//获取尾部信息

        model.addAttribute("info", info);
        model.addAttribute("head", head);//进行封装
        System.out.println(JSON.toJSON(head));
        model.addAttribute("footer", footer);
//        System.out.println(JSON.toJSON(footer));
        return info;
    }

    @GetMapping("/pub_head")
    public String pubHead(Model model,@RequestParam(value = "searchKey",required = false) String searchKey,
            @RequestParam(value="pageSize",defaultValue = "6")Integer pageSize,
            @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum) {

        PageUtils.startPage(pageNum,pageSize);
        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        PageUtils.orderBy("sort desc,id desc");

        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort));
        //搜索
        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsSiteModule m : modules) {
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            if(searchKey!=null){
                paramBanner.setTitle(searchKey);
            }

            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort));
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            moduleInfos.add(r);
        }
        model.addAttribute("modules", moduleInfos);
//        System.out.println(JSON.toJSON(moduleInfos));
        return "search";
    }

    @GetMapping("/pub_footer")
    public String pubFooter(Model model,@RequestParam(value = "searchKey",required = false) String searchKey) {
        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort));
        //搜索
        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsSiteModule m : modules) {
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            if(searchKey!=null){
                paramBanner.setTitle(searchKey);
            }

            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort));
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            moduleInfos.add(r);
        }
        model.addAttribute("modules", moduleInfos);
//        System.out.println(JSON.toJSON(moduleInfos));
        return "search";
    }

    @GetMapping("/index")
    public String index(Model model, @RequestParam(value = "searchKey", required = false) String searchKey) {
        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort));

        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsSiteModule m : modules) {
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            paramBanner.setModuleId(m.getId());
            paramBanner.setStatus(Constants.SUCCESS);
            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort));
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            moduleInfos.add(r);
        }
        model.addAttribute("modules", moduleInfos);
        System.out.println(JSON.toJSON(moduleInfos));
        return "index";
    }
    @GetMapping("/blog")
    public String blog(Model model) {

        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort));

        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsSiteModule m : modules) {
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            paramBanner.setModuleId(m.getId());
            paramBanner.setStatus(Constants.SUCCESS);
            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort));
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            moduleInfos.add(r);
        }
        model.addAttribute("modules", moduleInfos);
//        System.out.println(JSON.toJSON(moduleInfos));
        return "blog";
    }

    @GetMapping("/aboutus")
    public String aboutUs(Model model) {
        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort));

        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsSiteModule m : modules) {
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            paramBanner.setModuleId(m.getId());
            paramBanner.setStatus(Constants.SUCCESS);

            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort));
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            moduleInfos.add(r);
        }
        model.addAttribute("modules", moduleInfos);
//        System.out.println(JSON.toJSON(moduleInfos));
        return "aboutus";
    }
    @GetMapping("/contact")
    public String contact(Model model) {
        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort));

        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsSiteModule m : modules) {
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            paramBanner.setModuleId(m.getId());
            paramBanner.setStatus(Constants.SUCCESS);
            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort));
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            moduleInfos.add(r);
        }
        model.addAttribute("modules", moduleInfos);
//        System.out.println(JSON.toJSON(moduleInfos));
        return "contact";
    }
    @GetMapping("/cms_message")
    @ResponseBody
    public int insertAbout(CmsSiteVisitorMessage cmsSiteVisitorMessage){
        return cmsSiteVisitorMessageService.insertCmsSiteVisitorMessage(cmsSiteVisitorMessage);
    }

    @GetMapping("/cms_detail")
    public String detail(Model model, @RequestParam(value = "id", required = true) String id) {
        CmsSiteBanner cmsSiteBanner = cmsSiteBannerService.selectCmsSiteBannerById(Long.parseLong(id));
        model.addAttribute("data", cmsSiteBanner);
        System.out.println(JSON.toJSON(cmsSiteBanner));
        return "detail";
    }

    @GetMapping("/limit")
    @ResponseBody
    public TableDataInfo limit(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize){
        System.out.println(pageNum);
        startPage();
        PageUtils.orderBy("sort desc,id desc");
        List<CmsSiteBanner> data = cmsSiteBannerService.selectCmsSiteBannerList(null);
        System.out.println(JSON.toJSON(data));
        return getDataTable(data);
    }
}
